import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:ecy_app_flutter/EcyRouter.dart';
import 'package:ecy_app_flutter/ecycommon/EcyRequestBaseManager.dart';
import 'package:ecy_app_flutter/routes/login/common/DateUtil.dart';
import 'package:ecy_app_flutter/routes/my/model/OrderCommentDto.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../../model/EcyApiCommonResponse.dart';
import '../model/EcyMyOrderCommodity.dart';

class EcyOrderEvaluateListPage extends StatefulWidget {
  List<EcyMyOrderCommodity> commodityList;

  EcyOrderEvaluateListPage({required super.key, required this.commodityList});

  @override
  _EcyOrderEvaluateListPageState createState() =>
      _EcyOrderEvaluateListPageState();
}

class _EcyOrderEvaluateListPageState extends State<EcyOrderEvaluateListPage> {
  _EcyOrderEvaluateListPageState();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF5F5F5),
      appBar: AppBar(
        backgroundColor: Colors.white,
        scrolledUnderElevation: 0,
        title: Text(
          '待评价',
          style: TextStyle(fontFamily: "PingFang SC",
              fontWeight: FontWeight.w700,
              fontSize: 16.sp,
              color: const Color(0xFF262626)),
        ),
        centerTitle: true,
      ),
      body: SingleChildScrollView(
        child: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {},
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: widget.commodityList.map((e){
                return _buildCommodityCard(e);
              }).toList(),
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildCommodityCard(EcyMyOrderCommodity commodity) {
    return GestureDetector(
        child: Card(
          color: Colors.white,
          elevation: 0,
          margin: const EdgeInsets.only(bottom: 8),
          child: Padding(
            padding: const EdgeInsets.all(8),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                _getImagesBody(commodity),
                const Padding(padding: EdgeInsets.only(top: 8)),
                SizedBox(
                  height: 32.w,
                  child: TextButton(
                      onPressed: () {
                        EcyRouter().push(
                            context,
                            EcyRouter.orderEvaluatePage,
                            commodity).then((newCommodity) {
                          if(newCommodity != null) {
                            setState(() {
                              commodity.evaluateId = newCommodity.evaluateId;
                            });
                          }
                        });
                      },
                      style: const ButtonStyle(
                        alignment: Alignment.center,
                        foregroundColor: WidgetStatePropertyAll(Color(0xFFEA4B9B)),
                        side: WidgetStatePropertyAll(
                            BorderSide(color: Color(0xFFEA4B9B), width: 1)),
                      ),
                      child: Text(
                        commodity.evaluateId != null ? '查看评价' : '去评价',
                        style: TextStyle(
                          fontFamily: "PingFang SC",
                          fontWeight: FontWeight.w500,
                          fontSize: 14.sp,
                          color: const Color(0xFFEA4B9B),
                          height: 1,
                          leadingDistribution:
                          TextLeadingDistribution.even, // 设置leading策略
                        ),
                      )),
                ),
              ],
            ),
          ),
        ),
        onTap: () {
          // 进入订单详情
          EcyRouter.push(context, EcyRouter.orderInfoPage, commodity.orderId);
        });
  }

  Widget _getImagesBody(EcyMyOrderCommodity commodity) {
    var defaultImage = Image.asset(
      "assets/images/app_icon.png",
      width: 80.w,
      height: 80.w,
    );
    return LayoutBuilder(
      builder: (context, constraints) {
        return Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 80.w,
              width: 80.w,
              child: ClipRRect(
                  borderRadius: BorderRadius.circular(8.r),
                  child: CachedNetworkImage(
                      width: 80.w,
                      height: 80.w,
                      imageUrl: commodity.image ?? "",
                      imageBuilder: (context, imageProvider) => Container(
                            decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(8.r)),
                                image: DecorationImage(
                                    image: imageProvider, fit: BoxFit.cover)),
                          ),
                      errorWidget: (context, url, error) => defaultImage)),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.only(left: 8),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child: Text(
                            commodity.name ?? '***',
                            style: TextStyle(
                              fontFamily: "PingFang SC",
                              fontWeight: FontWeight.w700,
                              fontSize: 14.sp,
                              color: const Color(0xFF262626),
                            ),
                            maxLines: 1,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ),
                      ],
                    ),
                    const SizedBox(height: 4),
                    Text(
                      commodity.description ?? '***',
                      style: TextStyle(
                        fontFamily: "PingFang SC",
                        fontWeight: FontWeight.w500,
                        fontSize: 14.sp,
                        color: const Color(0xFF8C8C8C),
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(
              width: 80.w,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text('￥${commodity.totalPrice}',
                      style: TextStyle(
                          fontFamily: "PingFang SC",
                          fontWeight: FontWeight.w900,
                          fontSize: 14.sp,
                          color: const Color(0xFFF55F4E)),
                      textAlign: TextAlign.right),
                  const SizedBox(height: 4),
                  Text('x${commodity.num ?? 0}',
                      style: TextStyle(
                          fontFamily: "PingFang SC",
                          fontWeight: FontWeight.w500,
                          fontSize: 14.sp,
                          color: const Color(0xFF8C8C8C)),
                      textAlign: TextAlign.right),
                ],
              ),
            ),
          ],
        );
      },
    );
  }
}
